package com.longcai.controller.customer;


import com.alibaba.fastjson.JSONObject;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjAddress;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.service.customer.ClyjAddressService;
import com.longcai.util.TokenUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-03-25
 */
@Controller
@RequestMapping("/clyjAddress")
public class ClyjAddressController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ClyjAddressService clyjAddressService;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 地址列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/getaddress")
    public ApiResult getaddress(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        return this.clyjAddressService.getaddress(clyjUser.getUserId());
    }

    /**
     * 新增、修改收货地址
     *
     * @param params
     * @return
     */
    @RequestMapping("/insertaddress")
    @ResponseBody
    public ApiResult<ClyjAddress> insertaddress(@RequestBody JSONObject params) {
        String addressid = params.getString("addressid");
        String token = params.getString("token");
        if (token == null || "".equals(token)) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        }
        if (addressid.equals("") || addressid == null) {
            return this.clyjAddressService.insertaddress(clyjUser.getUserId(), params);
        } else {
            return this.clyjAddressService.updateaddress(clyjUser.getUserId(), addressid, params);
        }

    }

    /**
     * 获取地址信息
     *
     * @param params
     * @return
     */
    @RequestMapping("/getAdsInfo")
    @ResponseBody
    public ApiResult<ClyjAddress> getAdsInfo(@RequestBody JSONObject params) {
        return this.clyjAddressService.getAdsInfo(params);
    }

    /**
     * 删除地址
     *
     * @param params
     * @return
     */
    @RequestMapping("/deladdress")
    @ResponseBody
    public ApiResult<ClyjAddress> deladdress(@RequestBody JSONObject params) {
        return this.clyjAddressService.deladdress(params);
    }
}

